<template>
  <div class="container">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="小智同学"
    ></van-nav-bar>
    <div class="chat-list">
      <div
        class="chat-item"
        v-for="(item, idx) in list"
        :key="idx"
        :class="item.name === 'xz' ? 'left' : 'right'"
      >
        <!-- template:它在逻辑上表示一个整体，但是，在渲染成dom之后， 不会生成一个具体的元素 -->
        <template v-if="item.name === 'xz'">
          <van-image
            fit="cover"
            round
            src="https://img.yzcdn.cn/vant/cat.jpeg"
          />
          <div class="chat-pao">{{ item.msg }}</div>
        </template>
        <template v-if="item.name === 'me'">
          <div class="chat-pao">{{ item.msg }}</div>
          <van-image fit="cover" round :src="userPhoto" />
        </template>
      </div>
    </div>
    <div class="reply-container van-hairline--top">
      <van-field v-model.trim="word" placeholder="说点什么...">
        <span @click="send()" slot="button" style="font-size:12px;color:#999"
          >提交</span
        >
      </van-field>
    </div>
  </div>
</template>

<script>
// 引入socket.io-client这个包提供的websocket功能
import io from "socket.io-client";
export default {
  name: "UserChat",
  data() {
    return {
      // 设计聊天记录
      list: [
        {
          name: "xz",
          msg: "你好，我是小智",
          timestamp: Date.now()
        },
        {
          name: "me",
          msg: "我是编程小王子！",
          timestamp: Date.now()
        },
        {
          name: "xz",
          msg: "你以为会在百度上抄代码，就是程序员了吗？",
          timestamp: Date.now()
        },
        {
          name: "me",
          msg: "不是这样吗？",
          timestamp: Date.now()
        }
      ], // 聊天记录
      word: "", // 你说话的内容
      socket: null
    };
  },
  computed: {
    userPhoto() {
      return this.$store.state.photo;
    }
  },
  created() {
    // console.log(this.$router);
    // 这里只是把socket当作一个普通的属性名，它不一定非常要写在data数据项中
    this.socket = io("http://47.114.163.79:3003", {
      query: {
        token: this.$store.state.tokenInfo.token
      }
    });
    //小智同学的事
    //1.监听连接成功  socket与服务器建立连接的回调事件
    this.socket.on("connect", () => {
      console.log("与websocket服务器连接成功");
      this.list.push({
        name: "xz",
        msg: "我准备好了",
        timestamp: Date.now()
      });
    });

    //2.on  监听服务器发过来的消息
    this.socket.on("message", obj => {
      console.log(obj, "监听服务器返回的内容");
      this.list.push({
        name: "xz",
        msg: obj.msg,
        timestamp: Date.now()
      });
      //理论  updated 更新完之后执行的  异步更新操作
      this.$nextTick(() => {
        this.toScrollBottom();
      });
    });
  },
  // updated() {

  // },
  //emit 是发布  on 是监听
  methods: {
    send() {
      console.log(this.word);
      //1.调用接口  使用socket.io 里面的emit 把this.word
      //meaasge 自定义事件  随便写
      this.socket.emit("message", {
        msg: this.word,
        timestamp: Date.now()
      });
      //2.在使用一个方法进行监听
      this.list.push({
        name: "me",
        msg: this.word,
        timestamp: Date.now()
      });
      //3.把服务器主动发过来的值渲染到页面上
      this.word = "";
      //经验
      // setTimeout(() => {
      //   this.toScrollBottom();
      // }, 0);
      //理论
      this.$nextTick(() => {
        this.toScrollBottom();
      });
    },
    //滚动条滚动到底部
    toScrollBottom() {
      //安全有效 10000
      // document.querySelector('.chat-list').scrollTop = 10000;
      document.querySelector(".chat-list").scrollTop = document.querySelector(
        ".chat-list"
      ).scrollHeight;
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
